from flask_appbuilder import Model
from flask_babel import lazy_gettext as _
import re
from myapp.utils import core

# 定义model
class MyappModelBase():

    label_columns={
        "name": "名称",
        "name_url": "名称",
        "name_title": "名称(移动鼠标，查看描述)",
        'job_type': '任务类型',
        "project": "项目组",
        "project_url":"项目组",
        "namespace": "命名空间",
        "namespace_url": "命名空间",
        "describe": "描述",
        "describe_url": "描述",
        "password": '密码',
        "workdir":"工作目录",
        "images": "镜像",
        "repository": "仓库",
        "args": "启动参数",
        "args_html": "启动参数",
        "demo": "参数示例",
        "demo_html": "参数示例",
        "entrypoint": '启动命令',
        "dockerfile": 'Dockerfile',
        "gitpath": 'git地址',
        "env": "环境变量",
        "privileged": "特权模式",
        "accounts": "k8s账号",
        "images_url": "镜像",
        "hostAliases": "host",
        "overwrite_entrypoint": "覆盖原始启动命令",
        "command": "启动命令",
        "working_dir": "启动目录",
        "volume_mount": "挂载目录",
        "node_selector": "调度机器",
        "image_pull_policy": "镜像拉取策略",
        "resource_memory": "内存申请",
        "resource_cpu": "cpu申请",
        "resource_gpu": "gpu申请",
        "resource":"资源",
        "timeout": "超时中断",
        "retry": "重试次数",
        "outputs": "输出",
        "version": "版本",
        "model_name":"模型名称",
        "model_version": "模型版本",
        "model_path":"模型地址",
        "model_name_url": "模型名称",
        "model_version_url": "模型版本",
        "model_path_url": "模型地址",
        "embedding_file_path":"embedding文件地址",
        "is_fallback":"兜底版本",
        "check_service":"检查服务",
        "status": "状态",
        "model_status":"状态",
        "status_url":"状态",
        "final_status":"最终状态",
        "pipeline": "任务流",
        "pipeline_url": "任务流",
        "etl_pipeline_url": "任务流",
        "service_pipeline_url": "推理编排",
        "run_id": "kfp运行id",
        "run_time": "kfp运行时间",
        "type": "类型",
        "reset": "重置",
        "user": "用户",
        "role": "角色",
        "dag_json": "流向图",
        "dag_json_html": "流向图",
        "username": "用户",
        "schedule_type": "调度类型",
        "cron_time": "调度周期",
        "global_args": "全局参数",
        "global_env": "全局环境变量",
        "parallelism": "任务并行数",
        "run_pipeline": "运行",
        "status_more": "状态详情",
        "status_more_html": "状态详情",
        "execution_date":"执行时间",
        "base_image":"基础镜像",
        "tag":"tag",
        "save":"保存",
        "history":"历史",
        "consecutive_build":"连续构建",
        "need_gpu":"需要gpu",
        "last_image":"最新镜像",
        "expired_limit":"有效实例数目",
        "canary":"分流",
        "shadow": "流量复制",
        "log": "日志",
        "pod": "容器",
        "ide_type": "IDE类型",
        "annotations": "注释",
        "annotations_html": "注释",
        "spec": "属性",
        "spec_html": "属性",
        "info_json": "通知",
        "info_json_html": "通知",
        "labels": "标签",
        "label": "标签",
        "labels_html": "标签",
        "label_url": "标签",
        "add_row_time": "添加时间",
        "experiment_id": "kfp实验id",
        "pipeline_file": "workflow yaml",
        "pipeline_file_html": "workflow yaml",
        "pipeline_argo_id": "kfp任务流id",
        "version_id": "kfp版本id",
        "job_template": "任务模板",
        "job_template_url": "任务模板",
        "template": "功能模板",
        "alert_status": "监控状态",
        "task_args":"任务参数",
        "url_html": "网址",
        "url": "网址",
        "public":"公开",

        "alert_user": "通知用户",
        "experiment": "Experiment yaml",
        "experiment_html": "Experiment yaml",
        "train_model": "训练模型",
        "ip": "ip",
        "deploy_time": "部署时间",
        "host": "域名",
        "host_url": "域名",
        "inference_host_url":"域名：测试(test.xx)/调试(debug.xx)",
        "deploy": "部署",
        "test_deploy": "测试部署",
        "prod_deploy": "生产部署",
        "check_test_service": '检测测试服务',
        "min_replicas": "最低副本数",
        "max_replicas": "最高副本数",
        "replicas": "副本数",
        "ports": "端口",
        "roll": "滚动发布",
        "k8s_yaml": "yaml",
        "service": "服务",
        "download_url": "下载地址",
        "metrics": "指标",
        "metrics_html":"指标",
        "operate_html":"操作",
        "md5": "md5",
        "service_type": "服务类型",
        "job_args_definition":"模板参数定义示例",
        "job_describe":"模板描述",
        "job_args_demo": "模板参数示例",
        "stop":"停止",
        "parallel_trial_count": "并行搜索次数",
        "max_trial_count": "最多搜索次数",
        "max_failed_trial_count": "最多失败搜索次数",
        "objective_type": "目标函数类型",
        "objective_goal": "目标值",
        "objective_metric_name": "目标度量",
        "objective_additional_metric_names": "附加目标度量",
        "algorithm_name": "搜索算法",
        "algorithm_setting": "搜索算法配置",
        "parameters": "超参数配置",
        "parameters_demo": "超参配置示例",
        "parameters_html": "超参数配置",
        "job_json": "搜索任务配置",
        "trial_spec": "任务 yaml",
        "trial_spec_html": "任务 yaml",
        "create_experiment": "启动调度",
        "run_instance":"运行实例",
        "monitoring": "监控",
        "monitoring_html":"监控",
        "monitoring_url":"监控",
        "link": "链接",
        "clear": "清理",
        "expand":"扩展",
        "expand_html":"扩展",
        "parameter": "扩展参数",
        "parameter_html": "扩展参数",
        "renew":"续期",
        "api_type":"接口类型",
        "code_dir":"代码目录",
        "id_url":"id",
        "debug": "调试",
        "run": "运行",
        "run_url":"运行",
        "depends_on_past":"过往依赖",
        "max_active_runs": "最大激活运行数",
        "des_image":"目标镜像",
        "target_image":"目标镜像",
        "image_history":"镜像历史",
        "elapsed_time":"耗时",
        "task_status":"任务状态",
        "hpa":"弹性伸缩容",
        "health":"健康检查",
        "transformer":"处理变换",
        "deploy_history":"部署记录",
        "model_input": "模型输入",
        "input_html":"输入",
        "model_output": "模型输出",
        "output_html": "输出",
        "config":"配置",
        "config_html": "配置",
        "app":"产品",
        "field": "领域",
        "cluster": "集群",
        "db_name":"数据库",
        "database":"数据库",
        "db": "数据库",
        "metadata_db": "数据库",
        "security_level":"安全等级",
        "value_score":"价值评分",
        "storage_size": "存储大小",
        "warehouse_level": "数仓类型",
        "crontab": "调度周期",
        "cost":"数据成本",
        "column_type": "列类型",
        "remark":"备注",
        "ttl": "保留时长",
        "sql": "sql",
        "sql_html": "sql",
        "primary_part_col_name":"主分区列",
        "app_group":"应用组",
        "sql_demo":"sql示例",
        "create_table_ddl":"建表sql",
        "insert_sql": "数据导入sql",
        "metric_type": "指标类型",
        "metric_data_type":"指标数据类型",
        "metric_dim": "指标维度",
        "metric_level": "指标级别",
        "metric_responsible":"指标责任人",
        "caliber":"口径",
        "is_partition":"是否分区列",
        "partition_type":"分区类型",
        "c_org_fullname":"组织架构",
        "lifecycle":"当前生命周期(天)",
        "rec_lifecycle":"推荐生命周期(天)",
        "storage_cost":"存储成本(元/月)",
        "visits_seven":"7日访问次数",
        "recent_visit":"最近访问日期",
        "partition_start":"分区开始时间",
        "partition_end":"分区结束时间",
        "visits_thirty":"30天访问次数",
        "product_name":"产品名",
        "product_id":"产品id",
        "product_desc":"产品描述",
        "active":"激活",
        "job_worker_image":"工作镜像",
        "job_worker_command": "启动命令",
        "source_type":"数据源类型",
        "priority":"优先级",
        "owner": "责任人",
        "industry":"行业",
        "etl_pipeline": "任务流",
        "etl_pipeline_id": "任务流id",
        "etl_task": "任务",
        "etl_task_id": "任务id",
        "access":"接入",
        "inference_config": "推理配置",
        "file_type":"文件类型",
        "responsible":"责任人",
        "cycle_unit":"周期单位",
        "task_type":"任务类型",
        "creator": "创建者",
        "created_by": "创建者",
        "changed_by": "修改者",
        "created_on": "创建时间",
        "create_time": "创建时间",
        "update_time":"更新时间",
        "changed_on": "修改时间",
        "change_time":"更新时间",
        "modified": "修改时间"
    }

    # 获取列的中文显示
    # @staticmethod
    def lab(col,label_columns=label_columns):
        if col in label_columns:
            return _(label_columns[col])
        return _(re.sub("[._]", " ", col).title())



    # 获取node选择器
    def get_default_node_selector(self,node_selector,resource_gpu,model_type):
        # 先使用项目中定义的选择器
        if not node_selector:
            node_selector=''

        # 不使用用户的填写，完全平台决定
        if core.get_gpu(resource_gpu)[0]:
            node_selector = node_selector.replace('cpu=true', 'gpu=true') + ",gpu=true,%s=true"%model_type
        else:
            node_selector = node_selector.replace('gpu=true', 'cpu=true') + ",cpu=true,%s=true"%model_type
        if 'org' not in node_selector:
            node_selector += ',org=public'
        node_selector = re.split(',|;|\n|\t', str(node_selector))
        node_selector = [selector.strip() for selector in node_selector if selector.strip()]
        node_selector = ','.join(list(set(node_selector)))
        return node_selector



